<template>
  <a-upload
    style="margin-left: 16px"
    name="file"
    :multiple="false"
    :data="{biz:bizPath}"
    :action="uploadAction"
    :headers="tokenHeader"
    :showUploadList="showUploadList"
    @change="handleChange">
    <a-button :disabled="fileDisabled">
      <a-icon type="upload"/>
      {{text}}
    </a-button>
  </a-upload>
</template>

<script>
  import Vue from 'vue'
  import { ACCESS_TOKEN } from "@/store/mutation-types"

  export default {
    name: "DtgUploadFile.vue",
    data(){
      return {
        tokenHeader: {'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)},
        uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",

      }
    },
    props: {
      text: {
        type: String,
        default: ()=>{
          return '文件上传'
        },
      },
      fileDisabled: {
        type: Boolean,
        default: false,
      },
      showUploadList: {
        type: Boolean,
        default: false,
      },
      /*这个属性用于控制文件上传的业务路径*/
      bizPath:{
        type:String,
        required:false,
        default:"temp"
      },
      fileList:{
        type: Array,
        default(){
          return []
        }
      },
    },
    methods:{
      beforeUpload(file) {
        /*var fileType = file.type;

        if (fileType.indexOf('image') > -1) {
          this.$message.warning('请上传文件');
          return false;
        }*/

        /*if (fileType === 'image') {
          if (fileType.indexOf('image') < 0) {
            this.$message.warning('请上传图片');
            return false;
          }
        } else if (fileType === 'file') {
          if (fileType.indexOf('image') >= 0) {
            this.$message.warning('请上传文件');
            return false;
          }
        }*/
        return true
      },
      handleChange(info){
        if (info.file.status === 'done') {
          if (info.file.response.success) {
              this.$emit('changeFile',info);
          } else {
            this.$message.error(`${info.file.response.message}`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`${info.file.response.message}`);
        }

        // this.$emit('changeFileList',info);
      },
    }
  }
</script>

<style scoped>

</style>